
 /**
 * 上海中赢金融信息服务有限公司
 * Copyright (c) 2017-2027 Chinazyjr,Inc.All Rights Reserved.
 */
package com.sys.midware.redis.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.sys.midware.assist.serialize.SerializeUtil;

/**
 * <b>Description：</b> 序列化工具类 <br/>
 * <b>ClassName：</b> SerializationUtils <br/>
 * <b>@author：</b> jackyshang <br/>
 * <b>@date：</b> 2016年8月23日 下午5:38:14 <br/>
 * <b>@version: </b>  <br/>
 */
public class SerializationUtils {

    @SuppressWarnings("unchecked")
    static <T extends Collection<?>> T deserializeValues(Collection<byte[]> rawValues, Class<T> type
            ) {
        // connection in pipeline/multi mode
        if (rawValues == null) {
            return null;
        }

        Collection<Object> values = (List.class.isAssignableFrom(type) ? new ArrayList<Object>(rawValues.size())
                : new LinkedHashSet<Object>(rawValues.size()));
        for (byte[] bs : rawValues) {
            values.add(SerializeUtil.unSerialize(bs));
        }
        return (T) values;
    }
    
//    @SuppressWarnings("unchecked")
//    public static <T> List<T> deserialize(byte[] rawValue) {
//        return s(rawValues, List.class);
//    }
    
    
    @SuppressWarnings("unchecked")
    public static <T> List<T> deserialize(List<byte[]> rawValues) {
        return deserializeValues(rawValues, List.class);
    }
    
    @SuppressWarnings("unchecked")
    public static <T> Set<T> deserialize(Set<byte[]> rawValues) {
        return deserializeValues(rawValues, Set.class);
    }
    
    
    @SuppressWarnings("unchecked")
    public static <T> Collection<T> deserialize(Collection<byte[]> rawValues) {
        return deserializeValues(rawValues, List.class);
    }
    
    @SuppressWarnings("unchecked")
    public static <T> Map<T, T> deserialize(Map<byte[], byte[]> rawValues) {
        if (rawValues == null) {
            return null;
        }
        Map<T, T> ret = new LinkedHashMap<T, T>(rawValues.size());
        for (Map.Entry<byte[], byte[]> entry : rawValues.entrySet()) {
            ret.put((T)SerializeUtil.unSerialize(entry.getKey()),(T)SerializeUtil.unSerialize(entry.getValue()) );
        }
        return ret;
    }
    
}
